package cn.lixin.basic.utils;

import cn.lixin.user.domain.Logininfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {
    public static Logininfo getCurrentLoginInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)){ // token 不为空 redis没有过期的时候
            WebApplicationContext webApplicationContext = WebApplicationContextUtils // 获取Spring容器
                    .getWebApplicationContext(request.getServletContext());
            Object redisTemplate = webApplicationContext.getBean("redisTemplate"); // 找到Spring中的redisTemplate
            RedisTemplate template = (RedisTemplate) redisTemplate; // 转为RedisTemplate
            Object logininfo = template.opsForValue().get(token); // 根据token获取logininfo
            return (Logininfo)logininfo;
        }
        return null;
    }
}
